package xin.marcher.module.diplomacy.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Repository;
import xin.marcher.module.diplomacy.domain.bo.ItemInfoBO;
import xin.marcher.module.diplomacy.domain.entity.*;
import xin.marcher.module.diplomacy.manager.custom.cache.ItemCollectCache;
import xin.marcher.module.diplomacy.mapper.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 商品 资源管理
 */
@Repository
public class ProductRepository {

    @Resource
    private SkuInfoMapper skuInfoMapper;
    @Resource
    private ItemShelfLifeMapper itemShelfLifeMapper;
    @Resource
    private ProducingAreaMapper producingAreaMapper;
    @Resource
    private SkuBoxSpecsMapper skuBoxSpecsMapper;
    @Resource
    private BrandInfoMapper brandInfoMapper;
    @Resource
    private BuyerInfoMapper buyerInfoMapper;
    @Resource
    private ItemVideoImgMapper itemVideoImgMapper;
    @Resource
    private QualityControlMapper qualityControlMapper;
    @Resource
    private SaleRuleMapper saleRuleMapper;
    @Resource
    private ItemCollectCache itemCollectCache;

    /**
     * 查询商品的详情信息
     *
     * @param skuId
     * @return
     */
    public ProductDetailDO queryProductDetail(String skuId) {
        // 查询商品的主图信息
        ProductDetailDO productDetailDO = skuInfoMapper.queryProductInfo(skuId);
        if (Objects.isNull(productDetailDO)) {
            return null;
        }
        // 查询item缓存相关信息，填充进去
        buildItemInfo(productDetailDO);
        // 查询商品的产地信息
        ProducingAreaDO producingArea = getProducingArea(productDetailDO.getProducingAreaId());
        // 商品的重量信息
        SkuBoxSpecsDO skuBoxSpecs = getSkuBoxSpecs(skuId);
        // 商品的品牌信息
        BrandInfoDO brandInfo = getBrandInfo(productDetailDO.getBrandId());
        // 商品的亮点信息
        SaleRuleDO saleRuleDO = getSaleRule(productDetailDO.getItemId());
        // 商品的质检报告
        List<QualityControlDO> qualityControlList = getQualityControlList(skuId);
        // 商品的主图详情信息
        List<ItemVideoImgDO> mainVideoImgList = getItemVideoImgList(productDetailDO.getItemId(), Arrays.asList(1, 3, 4));
        // 商品的轮播图信息
        List<ItemVideoImgDO> shufflingImgList = getItemVideoImgList(productDetailDO.getItemId(), Arrays.asList(2));
        // 商品的归属买家信息
        BuyerInfoDO buyerInfoDO = getBuyerInfo(String.valueOf(productDetailDO.getCategoryId()));
        // 构建商品的模型
        return buildProductDetailDO(productDetailDO,
                Objects.isNull(producingArea) ? null : producingArea.getProducingAreaName(),
                Objects.isNull(skuBoxSpecs) ? null : skuBoxSpecs.getWeightAttr().toPlainString(),
                Objects.isNull(brandInfo) ? null : brandInfo.getBrandName(),
                Objects.isNull(saleRuleDO) ? null : saleRuleDO.getHighlights(),
                qualityControlList,
                mainVideoImgList,
                shufflingImgList,
                buyerInfoDO);
    }

    /**
     * 填充item信息到商品详情中
     *
     * @param productDetailDO
     */
    private void buildItemInfo(ProductDetailDO productDetailDO) {
        Optional<List<ItemInfoBO>> itemOptional =
                itemCollectCache.listRedisStringData(true, Lists.newArrayList(productDetailDO.getItemId()));
        if (itemOptional.isPresent()) {
            ItemInfoBO itemInfoBO = itemOptional.get().get(0);
            productDetailDO.setItemName(itemInfoBO.getItemName());
            productDetailDO.setRecommend(itemInfoBO.getRecommend());
            productDetailDO.setItemType(itemInfoBO.getItemType());
            productDetailDO.setProducingAreaId(itemInfoBO.getProducingAreaId());
            productDetailDO.setItemStatus(itemInfoBO.getItemStatus());
            productDetailDO.setBrandId(itemInfoBO.getBrandId());
            productDetailDO.setShelfLife(itemInfoBO.getShelfLife());
            productDetailDO.setStoreConditionType(itemInfoBO.getStoreConditionType());
            productDetailDO.setCategoryId(itemInfoBO.getLastCategoryId());
            productDetailDO.setItemSpecsValue(itemInfoBO.getItemSpecsValue());
        }

    }

    /**
     * 构建 填充商品详情的信息
     *
     * @param productDetailDO
     * @param producingAreaName
     * @param goodsWeight
     * @param brandName
     * @param highlights
     * @param qualityControlList
     * @param mainVideoImgList
     * @param shufflingImgList
     * @param buyerInfoDO
     * @return
     */
    private ProductDetailDO buildProductDetailDO(ProductDetailDO productDetailDO,
                                                 String producingAreaName,
                                                 String goodsWeight,
                                                 String brandName,
                                                 String highlights,
                                                 List<QualityControlDO> qualityControlList,
                                                 List<ItemVideoImgDO> mainVideoImgList,
                                                 List<ItemVideoImgDO> shufflingImgList,
                                                 BuyerInfoDO buyerInfoDO) {
        productDetailDO.setProducingAreaName(producingAreaName);
        productDetailDO.setGoodsWeight(goodsWeight);
        productDetailDO.setBrand(brandName);
        productDetailDO.setHighlights(highlights);
        productDetailDO.setQualityControlList(qualityControlList);
        productDetailDO.setTeletextList(mainVideoImgList);
        productDetailDO.setVideoImgList(shufflingImgList);
        productDetailDO.setBuyer(buyerInfoDO);

        return productDetailDO;
    }

    /**
     * 获取商品的买手信息
     *
     * @param categoryId
     * @return
     */
    private BuyerInfoDO getBuyerInfo(String categoryId) {
        LambdaQueryWrapper<BuyerInfoDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(BuyerInfoDO::getCategoryId, categoryId);

        return buyerInfoMapper.selectOne(queryWrapper);
    }

    /**
     * 查询商品的某种图文信息
     *
     * @param itemId
     * @param contentTypeList
     * @return
     */
    private List<ItemVideoImgDO> getItemVideoImgList(String itemId, List<Integer> contentTypeList) {
        LambdaQueryWrapper<ItemVideoImgDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ItemVideoImgDO::getItemId, itemId);
        queryWrapper.in(ItemVideoImgDO::getContentType, contentTypeList);

        return itemVideoImgMapper.selectList(queryWrapper);
    }

    /**
     * 查询商品的亮点信息
     *
     * @param itemId
     * @return
     */
    private SaleRuleDO getSaleRule(String itemId) {
        LambdaQueryWrapper<SaleRuleDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SaleRuleDO::getItemId, itemId);

        return saleRuleMapper.selectOne(queryWrapper);
    }

    /**
     * 获取商品的质检报告详情
     *
     * @param skuId
     * @return
     */
    private List<QualityControlDO> getQualityControlList(String skuId) {
        LambdaQueryWrapper<QualityControlDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(QualityControlDO::getSkuId, skuId);
        return qualityControlMapper.selectList(queryWrapper);
    }

    /**
     * 查询商品的品牌信息
     *
     * @param brandId
     * @return
     */
    private BrandInfoDO getBrandInfo(Integer brandId) {
        return brandInfoMapper.selectById(brandId);
    }

    /**
     * 查询sku商品的规格信息
     *
     * @param skuId
     * @return
     */
    private SkuBoxSpecsDO getSkuBoxSpecs(String skuId) {
        LambdaQueryWrapper<SkuBoxSpecsDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SkuBoxSpecsDO::getSkuId, skuId);

        return skuBoxSpecsMapper.selectOne(queryWrapper);
    }

    /**
     * 查询产地信息
     *
     * @param producingAreaId
     * @return
     */
    private ProducingAreaDO getProducingArea(Integer producingAreaId) {
        return producingAreaMapper.selectById(producingAreaId);
    }

    /**
     * 查询保质期信息
     *
     * @param itemId
     * @return
     */
    private ItemShelfLifeDO getItemShelfLife(String itemId) {
        LambdaQueryWrapper<ItemShelfLifeDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ItemShelfLifeDO::getItemId, itemId);

        return itemShelfLifeMapper.selectOne(queryWrapper);
    }

}
